package com.dajie.pt.controller.edm;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dajie.common.framework.spring.controller.BaseController;
import com.dajie.corp.api.CorpServicesContext;
import com.dajie.corp.api.service.CorpService;
import com.dajie.corpreview.api.model.CorpreviewReviewDetail;
import com.dajie.corpreview.api.service.CorpReviewApiService;
import com.dajie.corpreview.client.CorpReviewServiceFactory;
import com.dajie.infra.user.model.UserBase;

@Controller
@RequestMapping("/edmtoast")
public class EdmToastController extends BaseController {

	protected static final Logger LOGGER = LoggerFactory.getLogger(EdmToastController.class);
	
	CorpService corpService = CorpServicesContext.getInstance().getCorpService();
	CorpReviewApiService corpReviewApiService = CorpReviewServiceFactory.getInstance().getCorpReviewApiService();
		
	@RequestMapping(value = "{edmId}/index")
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable(value = "edmId") Integer edmId,
			@RequestParam(value = "corpid", required = true) Integer corpId,
			@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "ismember", required = true) Integer ismember
			) {
		
		LOGGER.info("====================" + this.getClass().getSimpleName());

		UserBase userBase = this.getLoginUser();
		if(userBase != null)
		{
			uid = userBase.getUid();
		}
		ModelAndView mv=null;
		
		if(edmId!= null&&edmId==1){
			mv=new ModelAndView("edm/toast_reason");
		}		
		if(edmId!= null&&edmId==2){
			mv=new ModelAndView("edm/toast_change");	
		}
		if(edmId!= null&&edmId==3){
			mv=new ModelAndView("edm/toast_pay");
		}
		
		long st = System.currentTimeMillis();
		long scoreSum=0;
		long impressionCareerSum=0;
		long impressionSkillSum=0;
		long impressionAtmosSum=0;
		long impressionPressureSum=0;
		long impressionProspectSum=0;
		//int status=0;
		
		String corpName = corpService.getCorpBaseById(corpId).getName();
		
		//得到初始投票数
		//EdmCorpReviewInfo edmCorpReviewInfo=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,status);
		//if(edmCorpReviewInfo==null){	
			//edmCorpReviewService.insertEdmCorpReviewNumByCorpId(corpId,status);			
			//edmCorpReviewInfo=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,status);
		//}

		//得到公司初始评价
		Map<String, Object> map= corpReviewApiService.getCorpReviewMap(corpId);
		if (map != null && map.size() > 0) {
			if ((Integer) map.get("code") == 1) {
				scoreSum=Math.round((Double) map.get("scoreSum")*5);
				impressionCareerSum=Math.round((Double) map.get("impressionCareerSum")*5);
				impressionSkillSum=Math.round((Double) map.get("impressionSkillSum")*5);
				impressionAtmosSum=Math.round((Double) map.get("impressionAtmosSum")*5);
				impressionPressureSum=Math.round((Double) map.get("impressionPressureSum")*5);
				impressionProspectSum=Math.round((Double) map.get("impressionProspectSum")*5);
				
			}else{
				scoreSum=this.getRandomNum(2,4);
				impressionCareerSum=this.getRandomNum(2,4);
				impressionSkillSum=this.getRandomNum(2,4);
				impressionAtmosSum=this.getRandomNum(2,4);
				impressionPressureSum=this.getRandomNum(2,4);
				impressionProspectSum=this.getRandomNum(2,4);
			}
		}
		
		int cal1=getRandomNum(3,2);
		int cal2=getRandomNum(3,5);
		int cal3=getRandomNum(7,8);
		
		int agreeNum=getRandomNum(8, 9);
		int markNum=getRandomNum(7, 3);
		
	
		mv.addObject("corpName", corpName);
		mv.addObject("corpId", corpId);
		mv.addObject("scoreSum", scoreSum);
		mv.addObject("impressionCareerSum", impressionCareerSum);
		mv.addObject("impressionSkillSum", impressionSkillSum);
		mv.addObject("impressionAtmosSum", impressionAtmosSum);
		mv.addObject("impressionPressureSum", impressionPressureSum);
		mv.addObject("impressionProspectSum", impressionProspectSum);
		mv.addObject("ismember", ismember);
		mv.addObject("firstCal", cal1);
		mv.addObject("secondCal", cal2);
		mv.addObject("thirdCal", cal3);
		mv.addObject("agreeNum", agreeNum);
		mv.addObject("markNum", markNum);


		//mv.addObject("edmCorpReviewInfo", edmCorpReviewInfo);
		mv.addObject("uid", uid);
		LOGGER.info("EdmController.index.timecost:{}",(System.currentTimeMillis()-st));
		return mv;
	}


	private int getRandomNum(int a,int b){
		Random rd=new Random();//随机数生成器		
		return (int)(rd.nextDouble()*a)+b;
	}
	
/*	private String getCalendar(int min){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE,min);
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(cal.getTime());
		
	}*/
	
	@RequestMapping("{edmId}/corpreview")
	public ModelAndView corpReview(HttpServletRequest request,
			HttpServletResponse response,
			@PathVariable(value = "edmId") Integer edmId,
			@RequestParam(value = "reason", required = true) String reason,
			@RequestParam(value = "uid", required = true) Integer uid,
			@RequestParam(value = "corpName", required = true) String corpName,
			@RequestParam(value = "corpId", required = true) Integer corpId,
			@RequestParam(value = "impression", required = true) Integer impressionCorpScore,
			@RequestParam(value = "agree", required = true) Integer impressionCeoScore,
			@RequestParam(value = "career", required = true) Integer career,
			@RequestParam(value = "skill", required = true) Integer skill,
			@RequestParam(value = "workAtmos", required = true) Integer workAtmos,
			@RequestParam(value = "workPressure", required = true) Integer workPressure,
			@RequestParam(value = "prospect", required = true) Integer prospect,
			@RequestParam(value = "ismember", required = true) Integer isMember,
			@RequestParam(value = "firstCal", required = true) Integer firstCal,
			@RequestParam(value = "secondCal", required = true) Integer secondCal,
			@RequestParam(value = "thirdCal", required = true) Integer thirdCal,
			@RequestParam(value = "agreeNum", required = true) Integer agreeNum,
			@RequestParam(value = "markNum", required = true) Integer markNum
			) {
		
		UserBase userBase = this.getLoginUser();
		if(userBase != null)
		{
			uid = userBase.getUid();
		}
		

		int isAnonymous=0;
		//int status=0;
		ModelAndView mv=null;
		String topic="";
		
		if(edmId!= null&&edmId==1){
			mv=new ModelAndView("edm/reasonReview");
			topic="#离职原因#";
		}		
		if(edmId!= null&&edmId==2){
			mv=new ModelAndView("edm/changeReview");	
		}
		if(edmId!= null&&edmId==3){
			mv=new ModelAndView("edm/payReview");
			topic="#公司是否亏待你了#";
		}
		
		String reasonTopic=topic+reason;
		StringBuffer sbf=new StringBuffer(reasonTopic);
		for(int i=0;i<=(20-reasonTopic.length());i++){
		sbf.append(" ");
		}
		String reviewContent=sbf.toString();
		//EdmCorpReviewInfo edmCorpReviewInfo=edmCorpReviewService.getEdmCorpReviewByCorpId(corpId,status);
		CorpreviewReviewDetail corpReviewDetail=null;
		Map<String,Object> corpReviews=corpReviewApiService.addCorpReview(uid, corpId, isMember, impressionCorpScore, impressionCeoScore, isAnonymous, null, reviewContent);
		int[] impressions={career,skill,workAtmos,workPressure,prospect};
		Integer reviewId=null;
		if((Integer)corpReviews.get("code")==0){
			reviewId=(Integer) corpReviews.get("reviewId");
		}
		
		corpReviewApiService.impressionSave(uid, corpId, reviewId, impressions, reviewContent);
		List<Integer> idList=new ArrayList<Integer>();
		idList.add(reviewId);
		List<CorpreviewReviewDetail> reviewList=corpReviewApiService.getReviewListByIdList(idList);
		if(reviewList!=null&&!reviewList.isEmpty()){
			corpReviewDetail=reviewList.get(0);		
		}
				
		//mv.addObject("edmCorpReviewInfo", edmCorpReviewInfo);
		mv.addObject("reviewDetail", corpReviewDetail);
		mv.addObject("corpId", corpId);
		mv.addObject("corpName", corpName);
		mv.addObject("firstCal", firstCal);
		mv.addObject("secondCal", secondCal);
		mv.addObject("thirdCal", thirdCal);
		mv.addObject("agreeNum", agreeNum);
		mv.addObject("markNum", markNum);
		return mv;
	}
	
	
	}		

